package model;

import java.util.BitSet;
import java.util.Observable;

import datenbank.ModelStat1;
import datenbank.ModelStat10;
import datenbank.ModelStat11;
import datenbank.ModelStat12;
import datenbank.ModelStat2;
import datenbank.ModelStat3;
import datenbank.ModelStat4;
import datenbank.ModelStat5;
import datenbank.ModelStat6;
import datenbank.ModelStat7;
import datenbank.ModelStat8;
import datenbank.ModelStat9;

public class TCPModelSPS extends Observable {
	private volatile BitSet ioSenden = new BitSet(512);

	private volatile int[] intSendenParamater = new int[256];

	private volatile float[] floatSendenParamater = new float[128];

	private BitSet ioEmpf = new BitSet(512);

	private volatile int[] intEmpfParamater = new int[128];

	private volatile float[] floatEmpfParamater = new float[128];

	private ModelStat1 modelStat1;
	private ModelStat2 modelStat2;
	private ModelStat3 modelStat3;
	private ModelStat4 modelStat4;
	private ModelStat5 modelStat5;
	private ModelStat6 modelStat6;
	private ModelStat7 modelStat7;
	private ModelStat8 modelStat8;
	private ModelStat9 modelStat9;
	private ModelStat10 modelStat10;
	private ModelStat11 modelStat11;
	private ModelStat12 modelStat12;

	/**
	 * Konstruktor
	 * 
	 * @param modelAllgemein
	 * @param modelStat1
	 * @param modelStat2
	 * @param modelStat3
	 * @param modelStat4
	 * @param modelStat5
	 * @param modelStat6
	 * @param modelStat7
	 * @param modelStat8
	 * @param modelStat12
	 * @param modelStat11
	 * @param modelStat10
	 */
	public TCPModelSPS(ModelStat1 modelStat1, ModelStat2 modelStat2, ModelStat3 modelStat3, ModelStat4 modelStat4,
			ModelStat5 modelStat5, ModelStat6 modelStat6, ModelStat7 modelStat7, ModelStat8 modelStat8, ModelStat9 modelStat9,
			ModelStat10 modelStat10, ModelStat11 modelStat11, ModelStat12 modelStat12) {

		this.modelStat1 = modelStat1;
		this.modelStat2 = modelStat2;
		this.modelStat3 = modelStat3;
		this.modelStat4 = modelStat4;
		this.modelStat5 = modelStat5;
		this.modelStat6 = modelStat6;
		this.modelStat7 = modelStat7;
		this.modelStat8 = modelStat8;
		this.modelStat9 = modelStat9;
		this.modelStat10 = modelStat10;
		this.modelStat11 = modelStat11;
		this.modelStat12 = modelStat12;
	}

	/**
	 * 
	 */
	private void datenAuslInt() {
		int j = 0;

		// Station 1
		int k = 3;
		for (int i = 0; i < 10; i++) {
			intSendenParamater[j++] = modelStat1.getDBEintragInt(k);

			k += 2;
		}

		// Station 2
		k = 3;
		for (int i = 0; i < 10; i++) {
			intSendenParamater[j++] = modelStat2.getDBEintragInt(k);

			k += 2;
		}

		// Station 3
		k = 3;
		for (int i = 0; i < 10; i++) {
			intSendenParamater[j++] = modelStat3.getDBEintragInt(k);

			k += 2;
		}

		// Station 4
		k = 3;
		for (int i = 0; i < 10; i++) {
			intSendenParamater[j++] = modelStat4.getDBEintragInt(k);

			k += 2;
		}

		// Station 5
		k = 3;
		for (int i = 0; i < 10; i++) {
			intSendenParamater[j++] = modelStat5.getDBEintragInt(k);

			k += 2;
		}

		// Station 6
		k = 3;
		for (int i = 0; i < 10; i++) {
			intSendenParamater[j++] = modelStat6.getDBEintragInt(k);

			k += 2;
		}

		// Station 7
		k = 3;
		for (int i = 0; i < 10; i++) {
			intSendenParamater[j++] = modelStat7.getDBEintragInt(k);

			k += 2;
		}

		// Station 8
		k = 3;
		for (int i = 0; i < 10; i++) {
			intSendenParamater[j++] = modelStat8.getDBEintragInt(k);

			k += 2;
		}

		// Station 9
		k = 3;
		for (int i = 0; i < 10; i++) {
			intSendenParamater[j++] = modelStat9.getDBEintragInt(k);

			k += 2;
		}

		// Station 10
		k = 3;
		for (int i = 0; i < 10; i++) {
			intSendenParamater[j++] = modelStat10.getDBEintragInt(k);

			k += 2;
		}

		// Station 11
		k = 3;
		for (int i = 0; i < 10; i++) {
			intSendenParamater[j++] = modelStat11.getDBEintragInt(k);

			k += 2;
		}

		// Station 12
		k = 3;
		for (int i = 0; i < 10; i++) {
			intSendenParamater[j++] = modelStat12.getDBEintragInt(k);

			k += 2;
		}

		// Allgemein
		intSendenParamater[230] = Integer.parseInt(modelStat1.getDBEintragString(1));
	}

	/*
	 * 
	 */
	private void datenAuslFloat() {
		int j = 0;

		// Station 1
		int k = 23;
		for (int i = 0; i < 10; i++) {
			floatSendenParamater[j++] = modelStat1.getDBEintragFloat(k);

			k += 2;
		}

		// Station 2
		k = 23;
		for (int i = 0; i < 10; i++) {
			floatSendenParamater[j++] = modelStat2.getDBEintragFloat(k);

			k += 2;
		}

		// Station 3
		k = 23;
		for (int i = 0; i < 10; i++) {
			floatSendenParamater[j++] = modelStat3.getDBEintragFloat(k);

			k += 2;
		}

		// Station 4
		k = 23;
		for (int i = 0; i < 10; i++) {
			floatSendenParamater[j++] = modelStat4.getDBEintragFloat(k);

			k += 2;
		}

		// Station 5
		k = 23;
		for (int i = 0; i < 10; i++) {
			floatSendenParamater[j++] = modelStat5.getDBEintragFloat(k);

			k += 2;
		}

		// Station 6
		k = 23;
		for (int i = 0; i < 10; i++) {
			floatSendenParamater[j++] = modelStat6.getDBEintragFloat(k);

			k += 2;
		}

		// Station 7
		k = 23;
		for (int i = 0; i < 10; i++) {
			floatSendenParamater[j++] = modelStat7.getDBEintragFloat(k);

			k += 2;
		}

		// Station 8
		k = 23;
		for (int i = 0; i < 10; i++) {
			floatSendenParamater[j++] = modelStat8.getDBEintragFloat(k);

			k += 2;
		}

		// Station 9
		k = 23;
		for (int i = 0; i < 10; i++) {
			floatSendenParamater[j++] = modelStat9.getDBEintragFloat(k);

			k += 2;
		}

		// Station 10
		k = 23;
		for (int i = 0; i < 10; i++) {
			floatSendenParamater[j++] = modelStat10.getDBEintragFloat(k);

			k += 2;
		}

		// Station 11
		k = 23;
		for (int i = 0; i < 10; i++) {
			floatSendenParamater[j++] = modelStat11.getDBEintragFloat(k);

			k += 2;
		}

		// Station 12
		k = 23;
		for (int i = 0; i < 10; i++) {
			floatSendenParamater[j++] = modelStat12.getDBEintragFloat(k);

			k += 2;
		}
	}

	/**
	 * 
	 */
	private void datenAuslBool() {
		setIO(64, modelStat5.getDBEintragBit(82));
		setIO(65, modelStat5.getDBEintragBit(83));
		setIO(66, modelStat5.getDBEintragBit(84));

	}

	/**
	 * 
	 * @param io
	 */
	public void setIOByte(byte[] io) {
		ioEmpf = BitSet.valueOf(io);
	}

	/**
	 * 
	 * @param index
	 * @return
	 */
	public boolean getIO(int index) {
		if (index < ioEmpf.size()) {
			return ioEmpf.get(index);
		}

		return false;
	}

	/**
	 * 
	 * @param index
	 * @param wert
	 */
	public void setIO(int index, boolean wert) {
		if (wert)
			ioSenden.set(index);
		else
			ioSenden.clear(index);
	}

	/**
	 * 
	 * @return
	 */
	public byte[] getIoByte() {
		datenAuslBool();

		byte[] b_copy = new byte[1];
		byte[] b = new byte[64];

		b_copy = ioSenden.toByteArray();

		for (int i = 0; i < b.length; i++) {
			if (i < b_copy.length)
				b[i] = b_copy[i];
			else {
				b[i] = 0;
			}
		}

		return b;
	}

	/**
	 * 
	 * @param parameter
	 */
	public void setIntParamter(int[] parameter) {
		intEmpfParamater = parameter;
	}

	/**
	 * 
	 * @param index
	 * @return
	 */
	public int getIntParameter(int index) {
		return intEmpfParamater[index];
	}

	/**
	 * 
	 * @param index
	 * @param wert
	 */
	public void setIntParameter(int index, int wert) {
		intSendenParamater[index] = wert;
	}

	/**
	 * 
	 * @return
	 */
	public int[] getIntParameter() {
		datenAuslInt();

		return intSendenParamater;
	}

	/**
	 * 
	 * @param parameter
	 */
	public void setFloatParamter(float[] parameter) {
		floatEmpfParamater = parameter;
	}

	/**
	 * 
	 * @param index
	 * @return
	 */
	public float getFloatParameter(int index) {
		return floatEmpfParamater[index];
	}

	/**
	 * 
	 * @param index
	 * @param wert
	 */
	public void setFloatParameter(int index, int wert) {
		floatSendenParamater[index] = wert;
	}

	/**
	 * 
	 * @return
	 */
	public float[] getFloatParameter() {
		datenAuslFloat();

		return floatSendenParamater;
	}
}